<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreDifficultyRequest;
use App\Models\Difficulty;
use App\Models\DifficultyType;
use App\Services\DifficultyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DifficultyController extends Controller
{
    protected $DifficultyService;
    public function __construct(DifficultyService $DifficultyService)
    {
        $this->DifficultyService = $DifficultyService;
    }

    public function index(Request $Request) {
        $type = $Request->type;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->DifficultyService->get($type);
        $sql->where('project_id',$Request->project_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }


    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Difficulty::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新数据。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $Request)
    {
        $POSTONLY = Difficulty::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        if($input['measure']){
            $input['person_id'] = Auth::user()->id;
        }
        $data = $this->DifficultyService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Difficulty::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $input['person_id'] = Auth::user()->id;
        $data = $this->DifficultyService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Difficulty::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function types(Request $Request) {
        $data = DifficultyType::where('project_id',$Request->project_id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新类型。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function storeType(Request $Request)
    {
        $POSTONLY = DifficultyType::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->DifficultyService->insertType($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function updateType(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = DifficultyType::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->DifficultyService->updateType($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function deleteType(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = DifficultyType::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
